package com.cefa.edu.service;

import com.cefa.edu.dao.ScoreMapper;
import com.cefa.edu.dao.UserMapper;
import com.cefa.edu.model.Score;
import com.cefa.edu.model.StudentInfo;
import com.cefa.edu.model.User;
import com.cefa.edu.model.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class AcademicRegisterService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    public List<StudentInfo> findStudentName(StudentInfo studentInfo) {
        String search = studentInfo.getSearchText();
        if ((studentInfo.getSearchText() == null || studentInfo.getSearchText().equals("")) && (studentInfo.getLevel_id() != null && !studentInfo.getLevel_id().toString().equals(""))) {
            return userMapper.findStudentName(studentInfo.getLevel_id().toString());
        } else if ((studentInfo.getSearchText() != null || !studentInfo.getSearchText().equals("")) && (studentInfo.getLevel_id() == null || studentInfo.getLevel_id().toString().equals(""))) {
            return searchStudent(studentInfo.getSearchText(), studentInfo.getUser_id());
        } else if ((studentInfo.getSearchText() != null && !studentInfo.getSearchText().equals("")) && (studentInfo.getLevel_id() != null && !studentInfo.getLevel_id().toString().equals(""))) {
            List<StudentInfo> studentName = userMapper.findStudentName(studentInfo.getLevel_id().toString());
            ArrayList<StudentInfo> searchStudents = new ArrayList<>();
            for (StudentInfo student : studentName) {
                if (student.getName().contains(search)) {
                    searchStudents.add(student);
                }
            }
            return searchStudents;
        }
        return null;
    }

    public List<StudentInfo> findStudentInfo(Integer user_id) {
        return userMapper.findStudentInfoByLevelId(" 4 , 5 ");
    }

    public List<StudentInfo> searchStudent(String searchText, Integer user_id) {
        List<StudentInfo> studentsLike = null;
        List<StudentInfo> studentInfos = userMapper.findLoginUserLevel(user_id);
        HashSet<Integer> hashSet = new HashSet<>();
        for (StudentInfo studentInfo : studentInfos) {
            if (studentInfo.getLevel() == 1) {
                List<Integer> levelOneId = userMapper.findLevelOne(studentInfo.getId());
                hashSet.addAll(levelOneId);
            } else if (studentInfo.getLevel() == 2) {
                List<Integer> levelTwo = userMapper.findLevelTwo(studentInfo.getId());
                hashSet.addAll(levelTwo);
            } else if (studentInfo.getLevel() == 3) {
                hashSet.add(studentInfo.getId());
            }

            StringBuilder sb = new StringBuilder();
            if (!hashSet.isEmpty()) {
                for (Integer i : hashSet) {
                    sb.append(i).append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
            }
            studentsLike = userMapper.findStudentLevelId("%" + searchText + "%", sb.toString());
        }
        return studentsLike;
    }

    @Transactional("transactionManager")
    public Score saveStudentScore(Score score, Integer user_id) {
        User user = userMapper.findUserById(user_id);
        UserRole userRoleInfo = userMapper.getUserRoleInfo(user_id);
        score.setRecording_person(user.getName() + "(" + userRoleInfo.getRoleName() + ")");
        String recodingTime = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
        score.setRecording_time(recodingTime);
        scoreMapper.saveStudentScore(score);
        return score;
    }
}
